﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EzyChoice
{
    public class SimpleStructure : TreeNode
    {
        public double Weight
        {
            get
            {
                return weight;
            }

            set
            {
                weight = value;
                Text = cri.iName + " (" + weight + ")";
            }
        }
        private double weightBkp;
        private double weight;

        public Dictionary<Alternative, double> Score = new Dictionary<Alternative, double>();
        public List<Alternative> Alters = new List<Alternative>();
        private EzcItem cri;

        public string iName
        {
            get
            {
                return cri.iName;
            }
        }

        public void SetWeight(double weight)
        {
            if (this.Parent != null)
            {
                this.Weight = weight;

                double other = 1 - weight;

                if (other > 0)
                {
                    double sum = 0.0;

                    foreach (SimpleStructure s in this.Parent.Nodes)
                    {
                        if (s != this)
                        {
                            sum += s.weightBkp;
                        }
                    }

                    sum = sum > 0 ? sum : 0;

                    foreach (SimpleStructure s in this.Parent.Nodes)
                    {
                        if (s != this)
                        {
                            s.Weight = Math.Round(s.weightBkp * other / sum, 3);
                        }
                    }
                }
                else
                {
                    foreach (SimpleStructure s in this.Parent.Nodes)
                    {
                        if (s != this)
                        {
                            s.Weight = 0;
                        }
                    }
                }

            }
        }

        public void SetBkpWeight(double weight)
        {
            if (this.Parent != null)
            {
                this.Weight = weight;

                double sum = 0.0;

                foreach (SimpleStructure s in this.Parent.Nodes)
                {
                    sum += s.weightBkp;
                }

                sum = sum > 0 ? sum : 0;

                foreach (SimpleStructure s in this.Parent.Nodes)
                {
                    s.Weight = s.weightBkp / sum;
                }

            }
        }

        public void RollBackWeight()
        {
            this.Weight = this.weightBkp;

            foreach (SimpleStructure s in this.Nodes)
            {
                s.RollBackWeight();
            }
        }

        public SimpleStructure()
        {

        }

        public SimpleStructure(List<Alternative> alts, EzcItem criteria, double weight)
        {
            SetSimpleStructureInfo(alts, criteria, weight);
        }

        public void SetSimpleStructureInfo(List<Alternative> alts, EzcItem criteria, double weight)
        {
            this.Alters = alts;
            this.cri = criteria;
            this.Weight = weight;
            this.weightBkp = weight;

            foreach (Alternative a in alts)
            {
                this.Score.Add(a, 0.0);
            }
        }


        public void ConstructScore()
        {
            SimpleStructure parrent = Parent as SimpleStructure;

            if (this.Nodes.Count>0)
            {
                foreach (Alternative a in Alters)
                {
                    this.Score[a] = 0;
                }
            }
           
            foreach (SimpleStructure c in this.Nodes)
            {
                c.ConstructScore();
            }

            if (this.Nodes.Count == 0)
            {
                //ConstructChoice();
            }

            if (parrent != null)
            {
                foreach (Alternative a in Alters)
                {
                    parrent.Score[a] += Math.Round(this.Score[a] * Weight, 3);
                }
            }

        }

        private void ConstructChoice()
        {
            double sum = 0;
            foreach (Alternative a in this.Alters)
            {
                if (Score.ContainsKey(a))
                {
                    sum += Score[a];
                }
            }

            sum = sum > 0 ? sum : 1;

            foreach (Alternative a in this.Alters)
            {
                if (Score.ContainsKey(a))
                {
                    Score[a] = Score[a]/sum;
                }
            }
        }
    }
}
